/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inc.jmc.cyberboard.ui.print.ticket;

import inc.jmc.cyberboard.common.ImageLoader;

import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

/**
 *
 * @author hofner76
 */
public class TicketTemplateFrm extends javax.swing.JInternalFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6037131010671983656L;
	static Logger logger = Logger.getLogger(TicketTemplateFrm.class);
	private TicketTemplateVO ticketTemplateVO;
	String templateOnload="";
	
    /**
     * Creates new form TicketTemplateFrm
     */
    public TicketTemplateFrm() {
    	initComponents();
    	
        this.setSize(new Dimension(647,510));
        this.setFrameIcon(ImageLoader.images[ImageLoader.ICON_FRAME_MAIN]);
        if(!existTemplate()){
        	//Si no existe
        	ticketTemplateVO = this.getDefaultContent();
        	this.displayTicketInForm(ticketTemplateVO);
        	//this.updateMenuItemActionPerformed(null);
        	
        }else{
        	//si si existe entonces lo deserializamos
        	FileInputStream inputStream;
    		try {
    			//calculando la ruta
    			File file1=new File("");
    			String localDirectory;
    			localDirectory = ""+file1.getCanonicalPath();
    			String pathBatch1 = localDirectory+"\\opciones\\tickettemplate.ser";
    			
    			inputStream = new FileInputStream(pathBatch1);
    			ObjectInputStream objinput1= new ObjectInputStream(inputStream);
    			ticketTemplateVO=(TicketTemplateVO)objinput1.readObject();
    			objinput1.close();
    	        this.displayTicketInForm(ticketTemplateVO);
    	        this.updateMenuItemActionPerformed(null);
    	        templateOnload=this.templateTxt.getText();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        
        
    }
    public boolean existTemplate(){
    	boolean existe=false;
    	try {
			File file1=new File("");
			String localDirectory;
			localDirectory = ""+file1.getCanonicalPath();
			String pathBatch1 = localDirectory+"\\opciones\\tickettemplate.ser";
			File file = new File(pathBatch1);
			existe=file.exists();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return existe;
    	
    }
    
    public TicketTemplateVO getDefaultContent(){
    	TicketTemplateVO template=new TicketTemplateVO();
    	template.setNombre("Papeleria Alfaomega\n=============================");
    	template.setDomicilio("AV. TAMAULIPAS NO. 5 LOC. 101\nGustavo A. Madero CP.07010\nMexico DF\nRFC: OUE761332F87\n=============================");
        template.setTimeStamp("Ticket: numero\nLe atendio: empleado\nfechacompra\n=============================");
        template.setEncabezados("CANT DESC               MONTO\n=============================");
        template.setDetalle("1230 Articulo PRUa  11,115.00\n1230 Articuloeba    31,115.00\n=============================");
        template.setTotales("SUBTOTAL                29.75\nIVA                      5.25\nTOTAL                   35.00\n\nRECIBIDO                50.00\nCAMBIO                  15.00\n=============================");
        template.setPieDePagina("Fue un placer atenderle\nGracias por su compra\nPuede visitar nuestra pagina\nmipagina.com.mx");
        
        
        return template;
    }
    public void displayTicketInForm(TicketTemplateVO vo){
    	this.nombreTxt.setText(vo.getNombre());
    	this.direccionTxt.setText(vo.getDomicilio());
    	this.timeStampTxt.setText(vo.getTimeStamp());
    	this.cabeceraTxt.setText(vo.getEncabezados());
    	this.detalleTxt.setText(vo.getDetalle());
    	this.totalTxt.setText(vo.getTotales());
    	this.pieTxt.setText(vo.getPieDePagina());
    }
    public void getTicketFromForm(){
    	this.ticketTemplateVO.setNombre(this.nombreTxt.getText());
    	this.ticketTemplateVO.setDomicilio(this.direccionTxt.getText());
    	this.ticketTemplateVO.setTimeStamp(this.timeStampTxt.getText());
    	this.ticketTemplateVO.setEncabezados(this.cabeceraTxt.getText());
    	this.ticketTemplateVO.setDetalle(this.detalleTxt.getText());
    	this.ticketTemplateVO.setTotales(this.totalTxt.getText());
    	this.ticketTemplateVO.setPieDePagina(this.pieTxt.getText());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        nombreTxt = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        direccionTxt = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        timeStampTxt = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane8 = new javax.swing.JScrollPane();
        cabeceraTxt = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jPanel12 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        detalleTxt = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jPanel10 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        totalTxt = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        jScrollPane7 = new javax.swing.JScrollPane();
        pieTxt = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        templateTxt = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        guardarMenuItem = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        updateMenuItem = new javax.swing.JMenuItem();

        setClosable(true);
        setResizable(true);
        setTitle("Template de Recibo");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(300);

        jPanel2.setLayout(new java.awt.GridLayout(0, 1, 2, 0));

        jPanel7.setMaximumSize(new java.awt.Dimension(2147483647, 20));
        jPanel7.setMinimumSize(new java.awt.Dimension(105, 20));
        jPanel7.setPreferredSize(new java.awt.Dimension(299, 20));
        jPanel7.setLayout(new java.awt.BorderLayout());

        nombreTxt.setColumns(29);
        nombreTxt.setRows(5);
        jScrollPane2.setViewportView(nombreTxt);

        jPanel7.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jLabel3.setText("Nombre o razon social");
        jPanel7.add(jLabel3, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel7);

        jPanel6.setLayout(new java.awt.BorderLayout());

        direccionTxt.setColumns(29);
        direccionTxt.setRows(5);
        jScrollPane4.setViewportView(direccionTxt);

        jPanel6.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        jLabel2.setText("Domicilio");
        jPanel6.add(jLabel2, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel6);

        jPanel8.setLayout(new java.awt.BorderLayout());

        timeStampTxt.setEditable(false);
        timeStampTxt.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
        timeStampTxt.setColumns(29);
        timeStampTxt.setRows(5);
        jScrollPane3.setViewportView(timeStampTxt);

        jPanel8.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jLabel4.setText("Timestamp");
        jPanel8.add(jLabel4, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel8);

        jPanel9.setPreferredSize(new java.awt.Dimension(299, 40));
        jPanel9.setLayout(new java.awt.BorderLayout());

        cabeceraTxt.setColumns(29);
        cabeceraTxt.setRows(5);
        jScrollPane8.setViewportView(cabeceraTxt);

        jPanel9.add(jScrollPane8, java.awt.BorderLayout.CENTER);

        jLabel5.setText("Encabezados");
        jPanel9.add(jLabel5, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel9);

        jPanel12.setLayout(new java.awt.BorderLayout());

        detalleTxt.setEditable(false);
        detalleTxt.setBackground(new java.awt.Color(227, 227, 227));
        detalleTxt.setColumns(29);
        detalleTxt.setRows(5);
        jScrollPane5.setViewportView(detalleTxt);

        jPanel12.add(jScrollPane5, java.awt.BorderLayout.CENTER);

        jLabel8.setText("Detalle");
        jPanel12.add(jLabel8, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel12);

        jPanel10.setLayout(new java.awt.BorderLayout());

        totalTxt.setEditable(false);
        totalTxt.setBackground(new java.awt.Color(227, 227, 227));
        totalTxt.setColumns(29);
        totalTxt.setRows(5);
        jScrollPane6.setViewportView(totalTxt);

        jPanel10.add(jScrollPane6, java.awt.BorderLayout.CENTER);

        jLabel6.setText("Totales");
        jPanel10.add(jLabel6, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel10);

        jPanel11.setLayout(new java.awt.BorderLayout());

        pieTxt.setColumns(29);
        pieTxt.setRows(5);
        jScrollPane7.setViewportView(pieTxt);

        jPanel11.add(jScrollPane7, java.awt.BorderLayout.CENTER);

        jLabel7.setText("Pie de pagina");
        jPanel11.add(jLabel7, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel11);

        jSplitPane1.setLeftComponent(jPanel2);

        jPanel3.setLayout(new java.awt.BorderLayout());

        templateTxt.setEditable(false);
        templateTxt.setColumns(20);
        templateTxt.setRows(5);
        jScrollPane1.setViewportView(templateTxt);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel3);

        jPanel1.add(jSplitPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel4.setPreferredSize(new java.awt.Dimension(580, 22));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 580, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 22, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel4, java.awt.BorderLayout.PAGE_END);

        jPanel5.setPreferredSize(new java.awt.Dimension(580, 20));
        jPanel5.setLayout(new java.awt.BorderLayout());

        jLabel1.setText("Edita tu recibo y personalizalo como gustes");
        jPanel5.add(jLabel1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel5, java.awt.BorderLayout.PAGE_START);

        jMenu1.setText("File");

        guardarMenuItem.setText("Guardar template");
        guardarMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(guardarMenuItem);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        updateMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F11, 0));
        updateMenuItem.setText("Preview");
        updateMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateMenuItemActionPerformed(evt);
            }
        });
        jMenu2.add(updateMenuItem);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void updateMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateMenuItemActionPerformed
    	/*StringBuffer sb = new StringBuffer();
        sb.append(nombreTxt.getText()+"\n");
        sb.append(direccionTxt.getText()+"\n");
        sb.append(timeStampTxt.getText()+"\n");
        sb.append(cabeceraTxt.getText()+"\n");
        sb.append(detalleTxt.getText()+"\n");
        sb.append(totalTxt.getText()+"\n");
        sb.append(pieTxt.getText());*/
    	this.getTicketFromForm();
    	templateTxt.setText(ticketTemplateVO.toString());
    }//GEN-LAST:event_updateMenuItemActionPerformed

    private void guardarMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarMenuItemActionPerformed
    	FileOutputStream outputStream;
		try {
			//calculando la ruta
			File file1=new File("");
			String localDirectory;
			localDirectory = ""+file1.getCanonicalPath();
			String pathBatch1 = localDirectory+"\\opciones\\tickettemplate.ser";
			
			outputStream = new FileOutputStream(pathBatch1);
			ObjectOutputStream objoutput1= new ObjectOutputStream(outputStream);
	        objoutput1.writeObject(ticketTemplateVO);
	        objoutput1.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }//GEN-LAST:event_guardarMenuItemActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
    	if(!templateTxt.getText().equals(templateOnload) || !existTemplate()){
    		int opti = JOptionPane.showConfirmDialog(this, "Desea guardar los cambios?", "Guardar", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
        	if(opti==JOptionPane.NO_OPTION)
        		return;
        	else{
        		this.guardarMenuItemActionPerformed(null);
        		this.updateMenuItemActionPerformed(null);
        		
        		return ;
        	}
    		
    	}
    }//GEN-LAST:event_formInternalFrameClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea cabeceraTxt;
    private javax.swing.JTextArea detalleTxt;
    private javax.swing.JTextArea direccionTxt;
    private javax.swing.JMenuItem guardarMenuItem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea nombreTxt;
    private javax.swing.JTextArea pieTxt;
    private javax.swing.JTextArea templateTxt;
    private javax.swing.JTextArea timeStampTxt;
    private javax.swing.JTextArea totalTxt;
    private javax.swing.JMenuItem updateMenuItem;
    // End of variables declaration//GEN-END:variables

	public TicketTemplateVO getTicketTemplateVO() {
		return ticketTemplateVO;
	}
	public void setTicketTemplateVO(TicketTemplateVO ticketTemplateVO) {
		this.ticketTemplateVO = ticketTemplateVO;
	}
	
}
